# app.py
import sys, pprint
from pathlib import Path
from flask import Flask, jsonify

# 将项目根目录添加到系统路径
current_dir = Path(__file__).resolve().parent
sys.path.insert(0, str(current_dir))

from extensions import extentisons_init_app
from config import config
from models import *  # 导入所有模型

# 确保在导入视图之前导入模型
# try:
#     from views import user, project, file, misc
# except ImportError as e:
#     print(f"Import error: {e}")
#     raise
from views import user, project, file, misc


def create_app(config_name='default'):
    app = Flask(__name__)

    # 加载配置
    app.config.from_object(config[config_name])
    config[config_name].init_app(app)

    # # 初始化数据库
    extentisons_init_app(app)

    # 注册蓝图

    app.register_blueprint(user.user_bp)
    app.register_blueprint(project.project_bp)
    app.register_blueprint(file.file_bp)
    app.register_blueprint(misc.misc_bp)

    @app.route('/')
    def index():
        return jsonify({
            'message': 'Project Management System API',
            'endpoints': {
                'users': '/users',
                'projects': '/projects',
                'file_types': '/file_types',
                'design_phases': '/design_phases',
                'project_types': '/project_types'
            }
        })

    return app


if __name__ == '__main__':
    app = create_app()
    app.run(debug=True)
    pprint.pprint(sys.path)